package com.sugon.api;

import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.dao.SysMsgDao;
import com.sugon.entity.ApiResult;
import com.sugon.entity.SysUserEntity;
import com.sugon.service.SysMsgService;
import com.sugon.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "系统消息API")
@RestController
@RequestMapping("/api/v1/messages")
public class SystemMessageApiController extends ApiBaseAction {

    @Autowired
    private SysMsgService sysMsgService;
    @Autowired
    private SysMsgDao msgDao;

    /**
     * 获取当前用户所有消息列表API
     * @param loginUser
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "获取当前用户所有消息列表API")
    @PostMapping
    public ApiResult<List<Map<String, Object>>> querySpareParts(@ApiIgnore @LoginUser SysUserEntity loginUser) {
        ApiResult<List<Map<String, Object>>> apiResult = new ApiResult<>();
        return apiResult.success(sysMsgService.querySysMsgService(null, loginUser.getUserId()));
    }

    /**
     * 删除消息API
     * @param messageId
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "删除消息API")
    @PostMapping("/{messageId}/delete")
    @ApiImplicitParam(name = "messageId", value = "消息ID", paramType = "path", dataType = "String", required = true)
    @ResponseBody
    public ApiResult<String> batchDelete(@PathVariable("messageId") String messageId) {
        ApiResult<String> apiResult = new ApiResult<>();
        List<String> msgList = CollectionUtil.newArrayList(messageId);
        String whereSql = com.sugon.modules.utils.StrUtil.generateBatchSql(msgList, "ID", 1000);
        return msgDao.batchDelete(whereSql) > 0 ? apiResult.success() : apiResult.failMsg("删除失败");
    }

}
